import 'dart:async';
import 'dart:developer';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:fluwx/fluwx.dart';
import 'package:get/get.dart';
import 'package:sports_new/utils/app_icons.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/widgets/base/qkd_dialog.dart';
import 'package:weibo_kit/weibo_kit_platform_interface.dart';
import '../../../controller/service/third_login_service.dart';
import '../../../entity/other/third_auth_entity.dart';
import 'package:tencent_kit/tencent_kit.dart';
import '../../../resources/colours.dart';
import '../../../resources/styles.dart';
import '../../../server/api/user_api.dart';
import '../../../widgets/base/qkd_text.dart';

class MyAccountBindPage extends StatefulWidget {
  const MyAccountBindPage({super.key});

  @override
  State<MyAccountBindPage> createState() => _MyAccountBindPageState();
}

class _MyAccountBindPageState extends State<MyAccountBindPage> {
  late final WeChatResponseSubscriber _wechatListener;
  late final StreamSubscription<TencentResp> _qqListener;
  late final StreamSubscription<BaseResp> _wbListener;
  final thirdLoginService = Get.find<ThirdLoginService>();
  bool wxBind = false;
  bool wbBind = false;
  bool qqBind = false;
  bool appleBind = false;
  List<ThirdAuthEntity> list = [];

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      getData();
    });
    _wechatListener = (response) {
      if(response is WeChatAuthResponse){
        if(response.errCode == 0) {
          thirdBind(wxCode: response.code);
        }
      }
    };
    Fluwx().addSubscriber(_wechatListener);
    _qqListener = TencentKitPlatform.instance.respStream().listen((event) {
      log('$event');
      if (event is TencentLoginResp) {
        TencentLoginResp auth = event;
        if (auth.ret == 0) {
          thirdBind(qqAccessToken: auth.accessToken, qqOpenid: auth.openid);
        }
      }
    });
    _wbListener = WeiboKitPlatform.instance.respStream().listen((event) {
      log('$event');
      if (event is AuthResp) {
        AuthResp auth = event;
        if (auth.errorCode == 0) {
          thirdBind(wbAccessToken: auth.accessToken, wbUid: auth.userId);
        }
      }
    });
  }

  @override
  void dispose() {
    Fluwx().removeSubscriber(_wechatListener);
    _qqListener.cancel();
    _wbListener.cancel();
    super.dispose();
  }

  getData() async {
    List<ThirdAuthEntity>? result = await UserApi.getAuthList();
    if (result != null) {
      list = result;
      bool wxBind1 = false;
      bool wbBind1 = false;
      bool qqBind1 = false;
      bool appleBind1 = false;
      for (ThirdAuthEntity data in result) {
        if (data.type == 1) {
          wxBind1 = true;
        } else if (data.type == 2) {
          wbBind1 = true;
        } else if (data.type == 3) {
          qqBind1 = true;
        } else if (data.type == 4) {
          appleBind1 = true;
        }
      }
      wxBind = wxBind1;
      wbBind = wbBind1;
      qqBind = qqBind1;
      appleBind = appleBind1;
    }
    update();
  }

  unbind(int type) async {
    int? result = await UserApi.thirdUnbind(type);
    if (result == 200) {
      getData();
    }
  }

  thirdBind(
      {String? qqAccessToken,
      String? qqOpenid,
      String? wbAccessToken,
      String? wbUid,
      String? iosIdentityToken,
      String? wxCode}) async {
    final result = await UserApi.thirdBind(
        qqAccessToken: qqAccessToken,
        qqOpenid: qqOpenid,
        wbAccessToken: wbAccessToken,
        wbUid: wbUid,
        iosIdentityToken: iosIdentityToken,
        wxCode: wxCode);
    if (result == 200) {
      getData();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Styles.appBar(
        title: QkdText('账号关联'),
      ),
      backgroundColor: Colours.scaffoldBg,
      body: Column(children: [
        // if (Platform.isIOS) ...[
        const SizedBox(height: 10),
        _cell(
          wxBind ? '解绑微信账号' : '绑定微信账号',
          wxBind
              ? list.where((element) => element.type == 1).first.name
              : '未绑定',
          onTap: () async {
            if (wxBind) {
              final result = await Get.dialog(QkdDialog.alert('确定要解除绑定吗？'));
              if (result == true) {
                unbind(1);
              }
            } else {
              thirdLoginService.onWechatLogin();
            }
          },
        ),
        // const Divider(height: 0.5, indent: 16, color: Color(0xffeeeeee)),
        // // ],
        // _cell(
        //     wbBind ? '解绑微博账号' : '绑定微博账号',
        //     wbBind
        //         ? list.where((element) => element.type == 2).first.name
        //         : '未绑定', onTap: () async {
        //   if (wbBind) {
        //     final result = await Get.dialog(QkdDialog.alert('确定要解除绑定吗？'));
        //     if (result == true) {
        //       unbind(2);
        //     }
        //   } else {
        //     thirdLoginService.onWeiboLogin();
        //   }
        // }),
        const Divider(height: 0.5, indent: 16, color: Color(0xffeeeeee)),
        _cell(
            qqBind ? '解绑QQ账号' : '绑定QQ账号',
            qqBind
                ? list.where((element) => element.type == 3).first.name
                : '未绑定', onTap: () async {
          if (qqBind) {
            final result = await Get.dialog(QkdDialog.alert('确定要解除绑定吗？'));
            if (result == true) {
              unbind(3);
            }
          } else {
            thirdLoginService.onQQLogin();
          }
        }),
        // if (Platform.isIOS) ...[
        //   const Divider(height: 0.5, indent: 16, color: Color(0xffeeeeee)),
        //   _cell(appleBind ? '解绑Apple ID' : '绑定Apple ID',
        //       appleBind ? '已绑定' : '未绑定', onTap: () async {
        //     if (appleBind) {
        //       final result = await Get.dialog(QkdDialog.alert('确定要解除绑定吗？'));
        //       if (result == true) {
        //         unbind(4);
        //       }
        //     } else {
        //       AuthorizationCredentialAppleID auth =
        //           await thirdLoginService.onAppleLogin();
        //       thirdBind(iosIdentityToken: auth.identityToken);
        //     }
        //   })
        // ]
      ]),
    );
  }

  _cell(String title, String? desc,
      {void Function()? onTap, bool arrow = true}) {
    final c = Container(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      color: Colors.white,
      height: 52,
      child: Row(
        children: [
          QkdText(title,fontSize: 16),
          const Spacer(),
          QkdText(desc ?? "", color: Colours.grey99),
          if (arrow) ...[
            const SizedBox(width: 10),
            AppIcons.rightArrow()
          ]
        ],
      ),
    );
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: onTap,
      child: c,
    );
  }
}
